package com.newhero.app.report.web.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.newhero.product.common.controller.BaseController;
import com.newhero.product.common.mybatis.mapper.EntityWrapper;
import com.newhero.product.common.util.StringUtils;
import com.newhero.product.common.vo.Json;
import com.newhero.product.common.mybatis.plugins.Page;
import com.newhero.app.report.persistence.entity.ReportRegion;
import com.newhero.app.report.service.IReportRegionService;
import com.newhero.product.util.ExportUtil;

 /**
 * @ClassName: ReportRegionController
 * @Description: 访问控制类
 * @author zlf
 * @date 2018-09-11
 */
@Controller
@RequestMapping("/report/reportRegion")
public class ReportRegionController extends BaseController {
    
    @Autowired 
    private IReportRegionService reportRegionServiceImpl;
    														
    /**
     * 列表页面
     * @return
     */    
	@RequiresPermissions("/report/reportRegion/reportRegionList") 
	@RequestMapping(value = "/reportRegionList", method ={RequestMethod.GET,RequestMethod.POST })
	public String reportRegionList(
			@RequestParam(value = "page", defaultValue = "1", required = true) int page,// 页号
			@RequestParam(value = "rows", defaultValue = "" + _ROWS_PER_PAGE, required = true) int rows,// 每页显示的记录条数
			@RequestParam(value = "orderField", defaultValue = "report_region_id", required = true) String orderField,
			@RequestParam(value = "isAsc", defaultValue = "0", required = true) int isAsc,//1-代表true;0-代表false
			ReportRegion reportRegion,
			HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		Page<ReportRegion> p = new Page<ReportRegion>();
		p.setCurrent(page);
		p.setSize(rows);
		p.setOrderByField(orderField);
		if(isAsc==0){
			p.setAsc(false);
		}else{
			p.setAsc(true);
		}
		
		EntityWrapper<ReportRegion> entityWrapper = new EntityWrapper<ReportRegion>();
		
			
    	if(StringUtils.isNotBlank(reportRegion.getRegionName())){
			entityWrapper.like("region_name", reportRegion.getRegionName());
		}
		model.addAttribute("regionName", reportRegion.getRegionName());
		
			
    	if(StringUtils.isNotBlank(reportRegion.getRegionCode())){
			entityWrapper.like("region_code", reportRegion.getRegionCode());
		}
		model.addAttribute("regionCode", reportRegion.getRegionCode());
		
		
		p = reportRegionServiceImpl.selectPage(p, entityWrapper);
		model.addAttribute("data", p);
		model.addAttribute("orderField", orderField);
		model.addAttribute("isAsc", isAsc);
		
		return "/report/reportRegion/reportRegionList";
	}
	
    /**
     * 表单页面
     * @return
     */
	@RequestMapping(value = "/reportRegionForm", method = RequestMethod.GET)
	public String reportRegionForm(String reportRegionId, HttpServletRequest request) {
		if (reportRegionId != null && !"".equals(reportRegionId)) {
			request.setAttribute("reportRegionId", reportRegionId);
			request.setAttribute("formData",reportRegionServiceImpl.selectById(reportRegionId));
		} else {
			request.setAttribute("reportRegionId", "");
			request.setAttribute("formData", new HashMap<String, Object>());
		}
		return "/report/reportRegion/reportRegionForm";
	}
    
    /**
     * 表单保存
     * @return
     */
	@RequestMapping(value = "/reportRegionSave", method = RequestMethod.POST)
	public String reportRegionSave(
			String reportRegionId, ReportRegion reportRegion, 
			HttpServletRequest request,HttpServletResponse response, ModelMap model) {
		try {
			if (!StringUtils.isBlank(reportRegionId)) {
				model.addAttribute("flag", "数据修改成功！");
				reportRegionServiceImpl.updateById(reportRegion);
			} else {
				model.addAttribute("flag", "数据添加成功！");
				reportRegionServiceImpl.insert(reportRegion);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
		}
		return "redirect:reportRegionList";
	}

    /**
     * 删除
     * @param ids
     * @return
     */
	@RequestMapping(value = "/reportRegionDelete", method = { RequestMethod.POST, RequestMethod.GET })
	public String reportRegionDelete(
			@RequestParam(value = "ids", required = true) String ids,
			HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		String[] idArr = ids.split(",");
		reportRegionServiceImpl.deleteBatchIds(Arrays.asList(idArr));
		model.addAttribute("flag", "数据删除成功！");
		return "redirect:reportRegionList";
	}     
    
    
    @ResponseBody
	@RequestMapping(value = "/reportRegionAjax", method = RequestMethod.POST)
	public Json reportRegionAjax(String reportRegionId) {
		ReportRegion reportRegion = reportRegionServiceImpl.selectById(reportRegionId);
		Json json = new Json();
		json.setSuccess(true);
		json.setObj(reportRegion);
		return json;
	}
	
	@RequestMapping(value = "/reportRegionExportExcel", method = { RequestMethod.GET, RequestMethod.POST })
	public void reportRegionExportExcel(
			ReportRegion reportRegion,
			HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		
		EntityWrapper<ReportRegion> entityWrapper = new EntityWrapper<ReportRegion>();
		
	    if(StringUtils.isNotBlank(reportRegion.getReportRegionId())){
			entityWrapper.like("report_region_id", reportRegion.getReportRegionId());
		}
		model.addAttribute("reportRegionId", reportRegion.getReportRegionId());
						
		List<ReportRegion> resultList = reportRegionServiceImpl.selectList(entityWrapper);
		
		ExportUtil.export(request, response, resultList,"导出Excel","report_region", 0, 2, 0, 16);
	}
}
